#!/bin/bash

# 指令管理工具

# 配置
COMMANDS_FILE="commands.txt"
TEMPLATES_DIR="prompts/templates/user"
SYSTEM_TEMPLATES_DIR="prompts/templates/system"

# 添加指令
add_command() {
    local name="$1"
    local description="$2"
    local pattern="$3"
    local template_content="$4"
    local template_path="$TEMPLATES_DIR/$name.txt"
    
    # 检查指令是否已存在
    if grep -q "^$name |" "$COMMANDS_FILE"; then
        echo "错误：指令 '$name' 已存在"
        return 1
    fi
    
    # 添加指令到 commands.txt
    echo "$name | $description | $template_path | $pattern" >> "$COMMANDS_FILE"
    
    # 创建模板文件并写入内容
    if [ -n "$template_content" ]; then
        echo "$template_content" > "$template_path"
    else
        touch "$template_path"
    fi
    
    echo "指令 '$name' 已添加"
    echo "模板文件已创建：$template_path"
}

# 删除指令
delete_command() {
    local name="$1"
    local template_path="$TEMPLATES_DIR/$name.txt"
    
    # 检查是否为系统指令
    if grep -q "^$name |.*$SYSTEM_TEMPLATES_DIR" "$COMMANDS_FILE"; then
        echo "错误：系统指令不可删除"
        return 1
    fi
    
    # 检查指令是否存在
    if ! grep -q "^$name |" "$COMMANDS_FILE"; then
        echo "错误：指令 '$name' 不存在"
        return 1
    fi
    
    # 从 commands.txt 中删除指令
    sed -i "/$name |/d" "$COMMANDS_FILE"
    
    # 删除模板文件
    if [ -f "$template_path" ]; then
        rm -f "$template_path"
        echo "模板文件已删除：$template_path"
    fi
    
    echo "指令 '$name' 已删除"
}

# 查询指令
query_command() {
    local name="$1"
    if [ "$name" = "all" ]; then
        cat "$COMMANDS_FILE"
    else
        grep "$name |" "$COMMANDS_FILE"
    fi
}

# 修改指令
update_command() {
    local name="$1"
    local new_description="$2"
    local new_pattern="$3"
    local new_template_content="$4"
    local template_path="$TEMPLATES_DIR/$name.txt"
    
    # 检查指令是否存在
    if ! grep -q "^$name |" "$COMMANDS_FILE"; then
        echo "错误：指令 '$name' 不存在"
        return 1
    fi
    
    # 更新 commands.txt
    sed -i "s|^$name |.*|$name | $new_description | $TEMPLATES_DIR/$name.txt | $new_pattern|" "$COMMANDS_FILE"
    
    # 更新模板文件内容
    if [ -n "$new_template_content" ]; then
        echo "$new_template_content" > "$template_path"
    fi
    
    echo "指令 '$name' 已更新"
}

# 主函数
main() {
    case "$1" in
        "add")
            # 使用 -t 选项传递模板内容
            if [ "$2" = "-t" ]; then
                add_command "$3" "$4" "$5" "$6"
            else
                add_command "$2" "$3" "$4"
            fi
            ;;
        "delete")
            delete_command "$2"
            ;;
        "query")
            query_command "$2"
            ;;
        "update")
            # 使用 -t 选项传递新模板内容
            if [ "$2" = "-t" ]; then
                update_command "$3" "$4" "$5" "$6"
            else
                update_command "$2" "$3" "$4"
            fi
            ;;
        *)
            echo "用法: $0 {add|delete|query|update} [-t] [参数...]"
            echo "  add [-t] <name> <description> <pattern> [template_content]"
            echo "  delete <name>"
            echo "  query <name|all>"
            echo "  update [-t] <name> <description> <pattern> [template_content]"
            exit 1
            ;;
    esac
}

main "$@"